﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SbsSW.SwiPlCs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Adventure.Model
{
    internal class Item
    {
        #region Constructors

        internal Item(string name, string textureName, Rectangle bounds)
        {
            _name = name;
            _textureName = textureName;
            _bounds = bounds;
            _lastBounds = bounds;
        }

        #endregion

        #region Private Fields

        private string _name;
        private string _textureName;
        private Rectangle _bounds;
        private Texture2D _texture;
        private int _lastUpdateTime = 0;
        private Rectangle _lastBounds;
        private bool _beingCarried = false;
        private bool _visible = false;

        #endregion

        #region Internal Methods

        internal void Initialize()
        {
            _texture = GraphicsResources.ContentManager.Load<Texture2D>(string.Format(@"Textures\{0}", _textureName));
        }

        internal void Update(GameTime gameTime, Scenario currentScenario, int time)
        {
            using (var visibleQuery = new PlQuery(string.Format("encosta({0}, {1}, {2})", _name,currentScenario.Name, time)))
                _visible = visibleQuery.NextSolution();

            using (var positionsQuery = new PlQuery(string.Format("posicao(X, Y, {0}, {1})", _name, time)))
            {
                var position = positionsQuery.SolutionVariables.First();
                var x = Convert.ToInt32(position["X"].ToString()) - (int)currentScenario.Offset.X;
                var y = Convert.ToInt32(position["Y"].ToString()) - (int)currentScenario.Offset.Y;
                _bounds = new Rectangle(x, y, _bounds.Width, _bounds.Height);
            }

            using (var carriedQuery = new PlQuery(string.Format("carregando({0}, {1}, S)", _name, time)))
            {
                var carried = carriedQuery.SolutionVariables.First();
                _beingCarried = carried["S"].ToString() == "1" ? true : false;
            }
        }

        internal void UpdateData(Scenario currentScenario, Scenario lastCurrentScenario, int time)
        {
            var a = PlQuery.PlCall(string.Format("retract(posicao({0}, {1}, {2}, {3}))", _lastBounds.X + lastCurrentScenario.Offset.X, _lastBounds.Y + lastCurrentScenario.Offset.Y, _name, _lastUpdateTime));
            var b = PlQuery.PlCall(string.Format("asserta(posicao({0}, {1}, {2}, {3}))", _bounds.X + currentScenario.Offset.X, _bounds.Y + currentScenario.Offset.Y, _name, time));

            var c = PlQuery.PlCall(string.Format("retract(carregando({0}, {1}, {2}))", _name, _lastUpdateTime, _beingCarried ? "1" : "0"));
            var d = PlQuery.PlCall(string.Format("asserta(carregando({0}, {1}, {2}))", _name, time, _beingCarried ? "1" : "0"));

            _lastBounds = _bounds;
            _lastUpdateTime = time;
        }

        internal void Draw(GameTime gameTime)
        {
            if (_visible)
            {
                GraphicsResources.SpriteBatch.Begin();
                GraphicsResources.SpriteBatch.Draw(_texture, _bounds, Color.White);
                GraphicsResources.SpriteBatch.End();
            }
        }

        #endregion
    }
}
